package com.mtung.redis.bloom;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhenguo.yao
 * @date 2020/12/10
 */
public class GuavaBloomFilter {
    /**
     * 在布隆中存放100万条数据
     */
    private static Integer MAX_SIZE = 1000000;

    public static void main(String[] args) {
        BloomFilter bloomFilter = BloomFilter.create(Funnels.integerFunnel(),MAX_SIZE,0.01);
        for (int i = 0; i < MAX_SIZE; i++) {
            bloomFilter.put(i);
        }
        // 从布隆中查询数据是否存在
        List<Integer> strings = new ArrayList<>();
        for (int j = MAX_SIZE; j < MAX_SIZE + 10000; j++) {
            if (bloomFilter.mightContain(j)) {
                strings.add(j);
            }
        }
        System.out.println("误判数量:" + strings.size());



    }

}
